from datetime import datetime

from app import db


class Cartoon(db.Model):
    """
    漫画
    """
    __tablename__ = "cartoon"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # 漫画id 漫画的唯一标识
    name = db.Column(db.String(255), nullable=False)  # 漫画名字 有可能类似
    introduce = db.Column(db.String(255), nullable=False)  # 漫画简介
    url_face = db.Column(db.String(512), nullable=False) # 漫画封面的路径
    athor_id = db.Column(db.Integer, db.ForeignKey("athor.id"))   # 漫画的作者
    class_id = db.Column(db.Integer, db.ForeignKey("classify.id"))  # 漫画的分类
    chapter = db.relationship('Chapter', backref='cartoon', lazy='dynamic')
    comments = db.relationship("Comment", lazy="dynamic")


class News(db.Model):
    """新闻"""
    __tablename__ = "news"

    id = db.Column(db.Integer, primary_key=True)  # 新闻编号
    title = db.Column(db.String(256), nullable=False)  # 新闻标题
    source = db.Column(db.String(64), nullable=False)  # 新闻来源
    digest = db.Column(db.String(512), nullable=False)  # 新闻摘要
    content = db.Column(db.Text, nullable=False)  # 新闻内容
    clicks = db.Column(db.Integer, default=0)  # 浏览量
    create_time = db.Column(db.DateTime, default=datetime.now)  # 记录的创建时间
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)  # 记录的更新时间
    def to_dict(self):
        news_dict = {
            "id": self.id,
            "title": self.title,
            "source": self.source,
            "digest": self.digest,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "clicks": self.clicks
        }
        return news_dict

    def to_collect_dict(self):
        resp_dict = {
            "id": self.id,
            "title": self.title,
            "create_time": self.create_time.strftime("%Y-%m-%d"),
        }

        return resp_dict
class Collection(db.Model):
    __tablename__ = "user_collection"
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"), primary_key=True)  # 新闻编号
    news_id = db.Column(db.Integer, db.ForeignKey("news.id"), primary_key=True)  # 分类编号
    create_time = db.Column(db.DateTime, default=datetime.now)  # 收藏创建时间

class Athor(db.Model):
    """漫画作者"""
    __tablename__ = "athor"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(20), nullable=False)
    cartoon = db.relationship('Cartoon', backref='athor', lazy='dynamic')


class Chapter(db.Model):
    """漫画章节"""
    __tablename__ = 'chapter'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(100), nullable=False)
    url = db.Column(db.String(255), nullable=False)
    cartoon_id = db.Column(db.Integer, db.ForeignKey("cartoon.id"))


class Classify(db.Model):
    """分类表"""
    __tablename__ = "classify"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    class_name = db.Column(db.String(20), nullable=False)
    cartoon = db.relationship('Cartoon', backref='classify', lazy='dynamic')


class User(db.Model):
    """用户"""
    __tablename__ = "user"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # 用户的id是唯一标识
    nick_name = db.Column(db.String(15), nullable=False)
    password_hash = db.Column(db.String(128), nullable=False)  # 加密的密码
    mobile = db.Column(db.String(11), unique=True, nullable=False)  # 手机号
    avatar_url = db.Column(db.String(256))  # 用户头像路径
    create_time = db.Column(db.DateTime, default=datetime.now)  # 注册时间
    last_login = db.Column(db.DateTime, default=datetime.now)  # 最后一次登录时间
    is_admin = db.Column(db.Boolean, default=False)
    signature = db.Column(db.String(512))  # 用户签名
    vip = db.Column(db.Integer, default=0)  # VIP等级 无上限
    vip_time = db.Column(db.DateTime)  # vip截至日期
    comments = db.relationship("Comment", backref="user")

    gender = db.Column(  # 性别
        db.Enum(
            "MAN",  # 男
            "WOMAN"  # 女
        ),
        default="MAN"
    )
    collection_news = db.relationship("News",
                                      secondary=Collection.__table__,
                                      backref=db.backref('collection_user', lazy='dynamic'),
                                      lazy='dynamic')

class Video(db.Model):
    """视频"""
    __tablename__ = 'video'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # 视频id 漫画的唯一标识
    name = db.Column(db.String(255), nullable=False)  # 视频名字
    url_face = db.Column(db.String(512), nullable=False)  # 视频的路径
    img_url = db.Column(db.String(512), nullable=False)  # 视频封面的路径
    class_id = db.Column(db.Integer, db.ForeignKey("video_classify.id"))  # 视频的分类
    video_vip = db.Column(db.Integer)

class Video_classify(db.Model):
    __tablename__ = 'video_classify'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # 分类id
    class_name = db.Column(db.String(255),nullable=False) # 分类名字
    video = db.relationship("Video", backref='video_classify',lazy='dynamic')


class Comment(db.Model):
    """评论"""
    __tablename__ = 'comment'
    id = db.Column(db.Integer, primary_key=True)  # 评论编号
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)  # 用户id
    cartoon_id = db.Column(db.Integer, db.ForeignKey("cartoon.id"), nullable=False)  # 漫画id
    content = db.Column(db.Text, nullable=False)  # 评论内容
    create_time = db.Column(db.DateTime, default=datetime.now)  # 记录的创建时间
    update_time = db.Column(db.DateTime, default=datetime.now)  # 记录的更新时间
    parent_id = db.Column(db.Integer, db.ForeignKey("comment.id"))  # 父评论id
    parent = db.relationship("Comment", remote_side=id)  # 自关联

    def to_basic_dict(self):
        ret = {
            "id": self.id,
            "user_id": self.user_id,
            "user_avatar_url": self.user.avatar_url,  # 这里有点绕，需要结合User类中的comments属性来研究
            "user_nick_name": self.user.nick_name,
            "content": self.content,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "parent": self.parent.to_basic_dict() if self.parent else None,
        }
        return ret


class RechargeCard(db.Model):
    __tablename__ = "recharge_card"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # 充值卡的id 主键 用来查看发布了多少张账号
    code_num = db.Column(db.String(23), nullable=False)  # 充值卡编码
    create_time = db.Column(db.DateTime, default=datetime.now)  # 创建时间
    user_time = db.Column(db.DateTime, default=datetime.now)  # 使用时间
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
    card_type = db.Column(db.Enum(
            "month",  # 月卡
            "season",  # 季卡
            "year"  # 年卡
        ),
        default="month")


